<?php
/**
 * @ Author: anchen
 * @ Date:   2016-04-29 18:27:31
 * @ Last Modified by:   anchen
 * @ Last Modified time: 2016-04-29 21:09:49
 * @ csv文件导入转成数组格式输出
 */
//header("Content-type:text/html;charset=utf-8");
//打开csv文件
$handle = fopen("./xls/aiwan91Charge.csv","r");
//定义csv转成数组的函数
function input_csv($handle)
{
    $out = array ();
    $n = $i = 0;
    while ($data = fgetcsv($handle, 10000))
    {
        //排除不需要的行(根据实际文件更改)
        if($n==0)
        {
            $n++;
            continue;
        }
        //获取必要的列项(根据实际文件更改)
        foreach($data as $k=>$v){
            if($k==0){
                $newdata['orderid'] = $v;//第一列的值
            }else if($k==1){
                $newdata['money'] = $v;//第二列的值
            }else if($k==2){
                $newdata['CharID'] = $v;//第三列的值
            }else if($k==3){
                $newdata['Time'] = $v;//第四列的值
            }else if($k==4){
                $newdata['GoldenBefore'] = $v;//第五列的值
            }else if($k==5){
                $newdata['GoldenAfter'] = $v;//第六列的值
            }else if($k==6){
                $newdata['MoneyBefore'] = $v;//第七列的值
            }else if($k==7){
                $newdata['MoneyAfter'] = $v;
            }else if($k==8){
                $newdata['ForceBefore'] = $v;
            }else if($k==9){
                $newdata['ForceAfter'] = $v;
            }else if($k==10){
                $newdata['BindGoldenBefore'] = $v;
            }else if($k==11){
                $newdata['BindGoldenAfter'] = $v;
            }
        }
        $out[$i] = $newdata;
        $i++;
    }
    //返回最终数组
    return $out;
}
//调用函数
$array = input_csv($handle);
//以字符串方式组成需要的格式
$str = "return array(<br/>";
foreach($array as $k=>$v){
    $str .= "'".$k."'=>array(";
    foreach($v as $vk=>$vv){
        $str .= "'".$vk."'=>'".$vv."',";
    }
    $str .= "),<br/>";
}
$str .= ");";
//输出字符串
echo $str;

/*
 * @ var_export()函数也可以输出类似array(1=>'a',2=>'b');的格式
 * @ 需要注意编码格式,最好不要和header("Content-type:text/html;charset=utf-8");同时使用
 */
//var_export($array);

?>